package io.renren.common.utils;

import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.io.File;

public class FileNameUtils {

    /**
     * 获取文件后缀
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName){
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 生成新的文件名
     * @param fileOriginName 源文件名
     * @return
     */
    public static String getUUIDFileName(String fileOriginName){
        return UUIDUtils.getUUID() + FileNameUtils.getSuffix(fileOriginName);
    }

    public static String getServerRootPath(){
        WebApplicationContext webApplicationContext = (WebApplicationContext)SpringContextUtils.applicationContext;
        ServletContext servletContext = webApplicationContext.getServletContext();
        String realPath = servletContext.getRealPath("/");
        String localPath = realPath + "upload" + File.separator +
                "files" + File.separator + "img";
        return localPath;
    }

}